package br.com.bolsamovel;

import java.util.Iterator;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class ListSelection {
	public static void selectOneItem(Context ctx, final List<?> l, String title, final CallBack cb){
		AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
		
		builder.setTitle(title);
		
		CharSequence[] items = new CharSequence[l.size()];
		int i = 0;
		
		for(Iterator<?> iterator = l.iterator(); iterator.hasNext();){
			items[i++] = iterator.next().toString();
		}
		
		builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		    	cb.onSelect(l.get(item), item);
		    	dialog.cancel();
		    }
		});
		
		AlertDialog alert = builder.create();
		
		alert.show();
	}

	public static interface CallBack{
		void onSelect(Object item, int index);
	}
}
